<?php
require "class.phpmailer.php" ;

define('MAILCONFPATH', ROOTDIR.'/config/send_mail.conf');

class SinaMail {
      private $SMTPServer;
      private $UserName;
      private $Password;
      private $From;

      function SinaMail() {
         if(($fp = fopen(MAILCONFPATH, "r"))) {
            while(($line = fgets($fp, 1024))) {
               $line = trim(chop($line));
               if($line[0] != "#") {
                  list($k, $v) = explode("=", $line);
                  $key   = trim($k); 
                  $value = trim($v);
                  if(!strcmp($key, "SMTPServer")) {
                     $this->SMTPServer = $value;
                  } else if(!strcmp($key, "UserName")) {
                     $this->UserName = $value;
                  } else if(!strcmp($key, "Password")) {
                     $this->Password = $value;
                  } else if(!strcmp($key, "From")) {
                     $this->From = $value;
                  }
               }
            }
            fclose($fp);
         }
      }

   public function send($subject, $mailList, $body, $attachment) {
      $mail = new PHPMailer();

      $mail->IsSMTP();                       // set mailer to use SMTP
      $mail->SMTPAuth = true;                // turn on SMTP authentication
      $mail->Host     = $this->SMTPServer;   // specify main and backup server
      $mail->Username = $this->UserName;     // SMTP username
      $mail->Password = $this->Password;     // SMTP password
      $mail->From     = $this->From;
      $mail->FromName = "KJAVAGROUP";

      $mail->IsHTML(true);                   // set email format to HTML
      $mail->CharSet  = "GBK";            // set mail charset
      $mail->WordWrap = 80;                  // set word wrap to 80 characters

      $mail->Subject  = $subject;            // mail subject
      foreach($mailList as $k=>$v) {
         $mail->AddAddress($v);              // name is optional
      }
      if(!empty($body)) {
         $mail->Body  = $body;               // set mail body
      }
      if(!empty($attachment)) {
         $mail->AddAttachment($attachment);  // add attachments
      }

      if($mail->Send()) {
         return true;
      } else {
         return false;
      }
   }
   
	public function useSysSendMail($to, $subject, $attachment){
		$uuencode = '/usr/bin/uuencode';
		$mail = '/bin/mail';
		$sendmail = $uuencode.' '.$attachment.' '.$attachment.'|'.$mail.' -s "'.$subject.'" -iIn '.$to;
		$res = `$sendmail`;
		return str_replace("\n", '', $res);
	}
 }
?>
